package com.zl.client.provider;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhouliang
 * @date 2019/11/25 10:21
 */
@Slf4j
@RestController
@RequestMapping("/provider")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProviderController {
    private final DiscoveryClient discoveryClient;

    /**
     * 测试eureka-client-provider服务能找到eureka server。
     * @return
     */
    @GetMapping(value = "/findInstance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<ServiceInstance> findInstance() {
        //测试 (eureka client) provider 是否可以找到eureka server
        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-server");
        log.info("根据服务名eureka-server获取到的服务实例明细:{}", JSON.toJSONString(instances));
        return instances;
    }
}
